<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 内容主体区域 -->
    <el-card class="box-card">
      <el-button type="primary" class="add" @click="addType">添加分类</el-button>
      <tree-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
        :show-row-hover="false"
      >
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color:lightgreen"></i>
          <i class="el-icon-error" v-else style="red"></i>
        </template>
        <template slot="grade" slot-scope="scope">
            <el-tag v-if="scope.row.cat_level === 0" size="mini">一级</el-tag>
            <el-tag type="success" v-else-if="scope.row.cat_level === 1" size="mini">二级</el-tag>
            <el-tag type="warning" v-else size="mini">三级</el-tag>
        </template>
        <template slot="handle">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>
     <!-- 分页区域 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="3"
        layout="total, sizes, prev, pager, next, jumper"
        :total= 'total'
      >
      </el-pagination>
    </div>
    </el-card>
    
    <el-dialog title="添加分类" :visible.sync="dialogVisible" width="50%" @close="closeAddCate">
        <!-- 添加分类的表单 -->
        <el-form :model="ruleForm" :rules="addtyperules" ref="ruleFormRef" label-width="100px">
            <el-form-item label="分类名称:" prop="cat_name">
                <el-input v-model="ruleForm.cat_name"></el-input>
            </el-form-item>
            <!-- options用来指定数据源 -->
            <el-form-item label="父级分类:">
                <el-cascader
                v-model="selectedKeys"
                :options="parentCateList"
                expand-trigger="hover"
                :props="cateProps"
                @change="handleChange"
                clearable
                >
                </el-cascader>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cateList: [],
      total: 0,
      dialogVisible: false,
      parentCateList: [],
    //   选中的父级分类的id数组
      selectedKeys: [],  
      cateProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      ruleForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0,
      },
      //表单验证规则
      addtyperules:{
        cat_name: [
            { required: true, message: '请输入分类名称', trigger: 'blur' },
        ],
      },
      columns: [
        {
          label: '分类名称',
          prop: "cat_name",
        },
        {
          //表示当前列是采用 template 模板
          label: "是否有效",
          type: "template",
          template: "isok",
        },
        {
            label:'排序',
            type: "template",
            template: 'grade'
        },
        {
            label:'操作',
            type:"template",
            template:'handle'
        }
      ],
      //查询条件
      query: {
        type: 3,
        pagenum: 1,
        pagesize: 3,
      },
    };
  },
  created() {
    this.getcatList();
  },
  methods: {
    async getcatList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.query,
      });
      if (res.meta.status !== 200) return this.$message.error("获取列表失败");
      this.cateList = res.data.result;
      this.total = res.data.total;
      console.log(this.cateList);
    },
    handleSizeChange(newsize) {
        this.query.pagesize = newsize
        this.getcatList()
    },
    handleCurrentChange(newnum) {
        this.query.pagenum = newnum
        this.getcatList()
    },
    //展示添加分类的对话框
    addType () {
        this.dialogVisible = true
        this.getParentCateList()
    },
    //获取父级分类列表
    async getParentCateList() {
        const { data: res } = await this.$http.get('categories', {params: { type: 2 }})
        // console.log(res);
        if (res.meta.status !== 200) return this.$message.error('获取父级分类失败')
        console.log(res.data);
        this.parentCateList = res.data
    },
    //选择项变化后,会触发此函数
    handleChange() {
        if(this.selectedKeys.length > 0) {
            //把 selected 数组中的最后一项 作为 他的父级ID值
            this.ruleForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
            //当前分类的等级就是选中的selected 的长度值  
            this.ruleForm.cat_level = this.selectedKeys.length
        } else {
            this.ruleForm.cat_pid = 0
            this.ruleForm.cat_level = 0
        }
    },
    addCate() {
        //验证输入的内容是不是符合要求,如果符合要求,那么久提交内容
        this.$refs.ruleFormRef.validate(async valid => {
            if(!valid) return this.$message.error('请输入内容') 
            const {data: res} = await this.$http.post('categories', this.ruleForm)
            if(res.meta.status !== 201) return this.$message.error('添加分类列表失败') 
            this.$message.success('添加分类成功')
            this.getcatList()
            this.dialogVisible = false
            console.log(res);
        })
    },
    closeAddCate () {
        this.$refs.ruleFormRef.resetFields()
        this.selectedKeys = []
        this.ruleForm.cat_pid = 0
        this.ruleForm.cat_level = 0
    }
  },
};
</script>


<style lang="less" scoped>
.add {
    margin-bottom: 15px;
}
// .el-form-item  {
//     width: 100%;
// }
</style>
